//
//  StockKLineQuotaView.m
//  K线图自治
//
//  Created by LK-IOS on 2017/12/13.
//  Copyright © 2017年 LK-IOS. All rights reserved.
//

#import "StockKLineQuotaView.h"
#import "UIColor+StockColorEntension.h"
@interface StockKLineQuotaView()

@property (nonatomic,strong) UILabel *currentPriceLabel;//当前价
@property (nonatomic , strong) UILabel *updownPriceLabel;//价格涨跌幅度
@property (nonatomic , strong) UILabel *updownPriceRateLabel;//价格涨跌百分比

@property (nonatomic,strong) UILabel *totalVolumeLabel;//总成交量
@property (nonatomic,strong) UILabel *totalVolumevalueLabel;//总成交额



@property (nonatomic,strong) UILabel *highLabel;
@property (nonatomic,strong) UILabel *lowLabel;
@property (nonatomic,strong) UILabel *openLabel;
@property (nonatomic,strong) UILabel *closeLabel;

@end

@implementation StockKLineQuotaView


-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self addSubviews];
        [self addConstraints];
    }
    return self;
}

-(void)addSubviews
{
    //当前价
    _currentPriceLabel = [self createLabel];
    _currentPriceLabel.font = [UIFont systemFontOfSize:font16ScreenWRate * LKScreenW];
    [self addSubview:_currentPriceLabel];
    
    //价格涨跌幅度
    _updownPriceLabel = [self createLabel];
    _updownPriceLabel.font = [UIFont systemFontOfSize:font12ScreenWRate * LKScreenW];
    [self addSubview:_updownPriceLabel];
    
    //价格涨跌百分比
    _updownPriceRateLabel = [self createLabel];
    _updownPriceRateLabel.font = [UIFont systemFontOfSize:font12ScreenWRate * LKScreenW];
    [self addSubview:_updownPriceRateLabel];
    
    //最高价
    _highLabel = [self createLabel];
    _highLabel.font = [UIFont systemFontOfSize:font12ScreenWRate * LKScreenW];
    [self addSubview:_highLabel];
    
    
    //最低价
    _lowLabel = [self createLabel];
    _lowLabel.font = [UIFont systemFontOfSize:font12ScreenWRate * LKScreenW];
    [self addSubview:_lowLabel];
    
    
    //开盘价
    _openLabel = [self createLabel];
    _openLabel.font = [UIFont systemFontOfSize:font12ScreenWRate * LKScreenW];
    [self addSubview:_openLabel];
    
    
    //收盘价
    _closeLabel = [self createLabel];
    _closeLabel.font = [UIFont systemFontOfSize:font12ScreenWRate * LKScreenW];
    [self addSubview:_closeLabel];
    
    
    //总成交量
    _totalVolumeLabel = [self createLabel];
    _totalVolumeLabel.font = [UIFont systemFontOfSize:font12ScreenWRate * LKScreenW];
    [self addSubview:_totalVolumeLabel];
    
    //总成交额
    _totalVolumevalueLabel = [self createLabel];
    _totalVolumevalueLabel.font = [UIFont systemFontOfSize:font12ScreenWRate * LKScreenW];
    [self addSubview:_totalVolumevalueLabel];
}

//控件宽度为屏的4分之1 ， 高为父控件的2分之1
-(void)addConstraints
{
    CGFloat margin = 10;
    CGFloat labelWidth = (LKScreenW - margin * 5) * 0.25;

    
    [_currentPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(margin);
        make.top.equalTo(self);
        make.width.equalTo(@(labelWidth));
        make.height.equalTo(self).multipliedBy(0.5);
    }];
    
    [_updownPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_currentPriceLabel);
        make.width.equalTo(@(labelWidth * 0.5));
        make.top.equalTo(_currentPriceLabel.mas_bottom);
        make.bottom.equalTo(self);
    }];
    
    
    [_updownPriceRateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_updownPriceLabel.mas_right);
        make.width.equalTo(@(labelWidth * 0.5));
        make.top.equalTo(_currentPriceLabel.mas_bottom);
        make.bottom.equalTo(self);
    }];
    
    
    
    [_highLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.width.equalTo(@(labelWidth));
        make.height.equalTo(self).multipliedBy(0.5);
        make.left.equalTo(_currentPriceLabel.mas_right).offset(margin);
    }];
    
    [_openLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_highLabel.mas_bottom);
        make.bottom.equalTo(self);
        make.width.equalTo(@(labelWidth));
        make.left.equalTo(_highLabel.mas_left);
    }];
    
    

    [_lowLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.width.equalTo(@(labelWidth));
        make.height.equalTo(self).multipliedBy(0.5);
        make.left.equalTo(_highLabel.mas_right).offset(margin);
    }];
    
    
    [_closeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(labelWidth));
        make.left.equalTo(_lowLabel.mas_left);
        make.bottom.equalTo(self);
        make.top.equalTo(_lowLabel.mas_bottom);
    }];
    
    
    
    [_totalVolumeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(labelWidth));
        make.top.equalTo(self);
        make.left.equalTo(_lowLabel.mas_right).offset(margin);
        make.height.equalTo(self).multipliedBy(0.5);
    }];
    
    
    [_totalVolumevalueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(labelWidth));
        make.bottom.equalTo(self);
        make.top.equalTo(_totalVolumeLabel.mas_bottom);
        make.left.equalTo(_totalVolumeLabel.mas_left);
    }];
    
    
}

/**
数据暂不处理，简单赋值显示
 */
-(void)setModel:(StockKLineModel *)model
{
    //总高度64
    _model = model;

    _currentPriceLabel.text = [NSString stringWithFormat:@"%.2f" , [model.currentPrice floatValue]];
    
    _updownPriceLabel.text = [NSString stringWithFormat:@"%.2f" , [model.priceUpDown floatValue]];
    
    _updownPriceRateLabel.text = [NSString stringWithFormat:@"%.2f%@" , [model.priceUpDownRate floatValue] * 100 , @"%"];
    
    if ([model.priceUpDown floatValue] > 0) {
        _currentPriceLabel.textColor = [UIColor Stock_PriceUpColor];
        _updownPriceRateLabel.textColor = [UIColor Stock_PriceUpColor];
        _updownPriceLabel.textColor = [UIColor Stock_PriceUpColor];
    }else{
        _currentPriceLabel.textColor = [UIColor Stock_PriceDownColor];
        _updownPriceRateLabel.textColor = [UIColor Stock_PriceDownColor];
        _updownPriceLabel.textColor = [UIColor Stock_PriceDownColor];
    }
    
    _highLabel.text = [NSString stringWithFormat:@"%@%.2f",@"高 : ",[model.High floatValue]];
    _lowLabel.text = [NSString stringWithFormat:@"%@%.2f",@"低 : ",[model.Low floatValue]];
    _openLabel.text = [NSString stringWithFormat:@"%@%.2f",@"开 : ",[model.Open floatValue]];
    _closeLabel.text = [NSString stringWithFormat:@"%@%.2f",@"收 : ",[model.Close floatValue]];
    
    _totalVolumeLabel.text = [NSString stringWithFormat:@"%@%.f手" , @"量:" , [model.Volume floatValue]];
    _totalVolumevalueLabel.text = [NSString stringWithFormat:@"%@%@" , @"额:" , model.VolumeValue];
    
}

-(UILabel*)createLabel
{
    UILabel *label = [UILabel new];
    label.textColor = [UIColor Stock_TextColor];
    label.text = @"---";
    label.textAlignment = NSTextAlignmentLeft;
    label.adjustsFontSizeToFitWidth = YES;
    return label;
}


/**
 计算一个字段的宽高
 */
- (CGRect)rectOfNSString:(NSString *)string attribute:(NSDictionary *)attribute
{
    CGRect rect = [string boundingRectWithSize:CGSizeMake(MAXFLOAT, 0)
                                       options:NSStringDrawingTruncatesLastVisibleLine |NSStringDrawingUsesLineFragmentOrigin |
                   NSStringDrawingUsesFontLeading
                                    attributes:attribute
                                       context:nil];
    return rect;
}



@end
